<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, student-scalable=no" />
<title>${siteName!""}|图书管理-编辑图书</title>
<#include "../common/header.ftl"/>

<!--时间选择插件-->
<link rel="stylesheet" href="/admin/js/bootstrap-datetimepicker/bootstrap-datetimepicker.min.css">
<!--日期选择插件-->
<link rel="stylesheet" href="/admin/js/bootstrap-datepicker/bootstrap-datepicker3.min.css">
<link href="/admin/css/style.min.css" rel="stylesheet">

</head>

<body>
<div class="lyear-layout-web">
  <div class="lyear-layout-container">
    <!--左侧导航-->
    <aside class="lyear-layout-sidebar">
      <div class="lyear-layout-sidebar-scroll">
        <#include "../common/left-menu.ftl"/>
      </div>
    </aside>
    <!--End 左侧导航-->
    <#include "../common/header-menu.ftl"/>
     <!--页面主要内容-->
    <main class="lyear-layout-content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-lg-12">
            <div class="card">
              <div class="card-header"><h4>编辑图书</h4></div>
              <div class="card-body">
                <form action="add" id="student-add-form" method="post" class="row">
                      <input type="hidden" name="id" id="id" value="${book.id}">
                    <#--<input type="hidden" name="studentId" value="${student.studentId}">-->
                    <div class="input-group m-b-10">
                    <span class="input-group-addon">图书名称</span>
                    <input type="text" class="form-control required" id="bookName" name="bookName" value="${book.bookName}" placeholder="请输入图书名称" tips="请填写图书名称" />
                  </div>
                    <div class="input-group m-b-10">
                        <span class="input-group-addon">图书作者</span>
                        <input type="text" class="form-control required" id="author" name="author" value="${book.author}" placeholder="请输入图书作者" tips="请填写图书作者" />
                    </div>

                    <div class="input-group m-b-10">
                        <span class="input-group-addon">图书单价</span>
                        <input type="text" class="form-control required" id="price" name="price" value="${book.price}" placeholder="请输入图书单价" tips="请填写图书单价" />
                    </div>

                    <div class="input-group m-b-10">
                        <span class="input-group-addon">出版社</span>
                        <input type="text" class="form-control required" id="press" name="press" value="${book.press}" placeholder="请输入出版社" tips="请填写出版社" />
                    </div>

                    <div class="input-group m-b-10">
                        <span class="input-group-addon">图书库存</span>
                        <input type="number" class="form-control required" min="1" onchange="if(/\D/.test(this.value)){this.value='1';}" id="cannum" name="cannum" value="${book.cannum}" placeholder="请输入图书库存" tips="请填写图书库存" />
                    </div>
                    <div class="input-group m-b-10">
                        <span class="input-group-addon">图书介绍</span>
                        <input type="text" class="form-control required" id="introduce" name="introduce" value="${book.introduce}" placeholder="请输入图书介绍" tips="请填写图书介绍" />
                    </div>
                    <div class="input-group m-b-10">
                        <span class="input-group-addon">所属分类</span>
                        <select name="parent.id" class="form-control" id="category" onchange="changeCate()">
                            <option value="0">---请选择---</option>
                         <#list categories2 as category>
                            <option value="${category.id}">${category.typeName}</option>
                         </#list>
                        </select>
                    </div>
                    <div class="input-group m-b-10">
                        <span class="input-group-addon">所属二级分类</span>
                        <select name="category"  class="form-control" id="parent">

                        </select>
                    </div>
                  <div class="form-group col-md-12">
                    <button type="button" class="btn btn-primary ajax-post" id="add-form-submit-btn">确 定</button>
                    <button type="button" class="btn btn-default" onclick="javascript:history.back(-1);return false;">返 回</button>
                  </div>
                </form>

              </div>
            </div>
          </div>

        </div>

      </div>

    </main>
    <!--End 页面主要内容-->
  </div>
</div>
<#include "../common/footer.ftl"/>
<script type="text/javascript" src="/admin/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="/admin/js/main.min.js"></script>

<script type="text/javascript" src="/admin/js/perfect-scrollbar.min.js"></script>
<!--时间选择插件-->
<script src="/admin/js/bootstrap-datetimepicker/moment.min.js"></script>
<script src="/admin/js/bootstrap-datetimepicker/bootstrap-datetimepicker.min.js"></script>
<script src="/admin/js/bootstrap-datetimepicker/locale/zh-cn.js"></script>
<!--日期选择插件-->
<script src="/admin/js/bootstrap-datepicker/bootstrap-datepicker.min.js"></script>
<script src="/admin/js/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js"></script>
<script type="text/javascript" src="/admin/js/main.min.js"></script>

<script type="text/javascript">
    var parentId = ${parentId};
    var category2Id = ${categoryId};
    var parentId2 = ${parentId2};

    function changeCate() {
        var categoryId = $("#category option:selected").val();
        $.ajax({
            url: "category",
            type: "POST",
            dataType: "json",
            // contentType : "application/json;charset=utf-8",
            data: {
                id:categoryId,
            },
            success: function (data) {
                console.log(data);
                var html = '<option value=\'\'>---请选择---</option>';
                $("#parent").empty();
                for (var i = 0, len = data.length; i < len; i++) {
                    html += "<option value='" + data[i].id +"'> " + data[i].typeName + "</option>";
                }
                $("#parent").append(html);

                if(category2Id != 0)
                {
                    $("#parent").val(category2Id);
                }
            },
            error: function (data) {
                // alert('网络错误!');
            }
        });
    }

    $(document).ready(function(){
	//提交按钮监听事件
	$("#add-form-submit-btn").click(function(){
		if(!checkForm("student-add-form")){
			return;
		}
		var data = $("#student-add-form").serialize();
		$.ajax({
			url:'edit',
			type:'POST',
			data:data,
			dataType:'json',
			success:function(data){
				if(data.code == 0){
					showSuccessMsg('图书编辑成功!',function(){
						window.location.href = 'list';
					})
				}else{
					showErrorMsg(data.msg);
				}
			},
			error:function(data){
				alert('网络错误!');
			}
		});
	});
	//监听上传图片按钮
	$("#add-pic-btn").click(function(){
		$("#select-file").click();
	});

	if(parentId != 0 || parentId2 != 0)
    {
        if(parentId2 != 0)
        {
            $("#category").val(parentId2);
        }
        else{
            $("#category").val(parentId);
        }

        changeCate();
    }


});
</script>
</body>
</html>
